<template>
  <el-card v-for="(item, index) in productList" :key="index">
    <el-row>
      <el-col :span="4">
        <el-image :src="item.picUrl" style="height: 130px;width: 100%"/>
      </el-col>
      <el-col :span="17">
        <div>
          <el-descriptions style="padding-top: 10px" :title=item.productName>
            <el-descriptions-item label="价格:">￥&nbsp;{{item.price}}&nbsp;元</el-descriptions-item>
            <el-descriptions-item label="品牌:">{{item.brandName}}</el-descriptions-item>
            <el-descriptions-item label="保质期:">{{item.shelfLife}}天</el-descriptions-item>
            <el-descriptions-item label="库存余额:">{{item.productCount}}</el-descriptions-item>
            <el-descriptions-item label="收藏日期:">{{item.productionDate}}</el-descriptions-item>
          </el-descriptions>
        </div>
      </el-col>
      <el-col :span="3">
        <div style="padding-top: 20px">
          <el-button type="danger" :icon="Delete" @click="DeleteFavorite(item.FavoriteId)" circle /><br><br><br>
          <el-button type="success" :icon="ShoppingCart" @click="Addcart(item)" circle /><br>
        </div>
      </el-col>
    </el-row>
  </el-card>
</template>

<script setup lang="ts" name="Favorite">
import {onMounted, ref} from "vue";
import useUserStore from "@/store/modules/user";
import {ResponseData} from "@/api/user/type";
import {reqDelateFavorite, reqFavoriteList} from "@/api/favorite";
import {reqProductInfo} from "@/api/product";
import {ElMessage} from "element-plus";
import {Delete,ShoppingCart} from '@element-plus/icons-vue'
import {reqAddCart} from "@/api/cart";
let UserStore = useUserStore();

const favoriteList =ref([]);
const productList =ref([]);
const init = ( async () =>{
  favoriteList.value = [];
  productList.value = [];
  var res : ResponseData = await reqFavoriteList({userId:UserStore.id});
  favoriteList.value = res.data;
  for (const item of favoriteList.value) {
    let res1: ResponseData  = await reqProductInfo(item.productId);
    res1.data.FavoriteId = item.id;
    res1.data.createTime = item.createTime;
    productList.value = productList.value.concat(res1.data);
  }
})
const DeleteFavorite =( async (item : any)=>{
  var res  =await  reqDelateFavorite({id:item});
  if(res.code == 0){
    init();
  }else {
    ElMessage.info(res.message);
  }
})
const Addcart = (async (item: any) =>{
  let AddCartRequestData ={
    userId : UserStore.id,
    productId : item.id,
    price : item.price,
    quantity : 1,
    totalPrice : item.price,
  }
  var res : ResponseData = await reqAddCart(AddCartRequestData);
  if(res.code == 0){
    ElMessage.success("已加入购物车");
  }else {
    ElMessage.info(res.message);
  }
})
onMounted(() => {
  init();
})
</script>


<style scoped lang="scss">

</style>